package kr.ac.jejuuniv.repository;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import kr.ac.jejuuniv.model.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ibatis.sqlmap.client.SqlMapClient;

@Repository
public class IbatisUserRepositoryImpl implements UserRepository {
	@Autowired
	private SqlMapClient sqlMapClient;

	public List<User> findAll() {
		try {
			return (List<User>)sqlMapClient.queryForList("User.selectUser");
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	public void insert(User user) {
		try {
			this.sqlMapClient.update("User.insertUser", user);
		} catch (SQLException e) {
			e.printStackTrace();
		}
//		jdbcDaoSupport.getJdbcTemplate().update("insert into users (id, name, password) values (?, ?, ?)", user.getId(), user.getName(), user.getPassword());
	}

	public User findById(String id) {
		Map<String, String> param =  new HashMap<String,String>();
		param.put("id", id);
		try {
			return (User)this.sqlMapClient.queryForObject("User.selectUser",param);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	public void update(User user) {
		try {
			this.sqlMapClient.update("User.updateUser",user);
		} catch (SQLException e) {
			e.printStackTrace();
		}
//		jdbcDaoSupport.getJdbcTemplate().update("update users set name = ?, password = ? where id = ?", user.getName(), user.getPassword(), user.getId());
	}



	public void delete(String id) {
		try {
			this.sqlMapClient.delete("User.deleteUser",id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
//		jdbcDaoSupport.getJdbcTemplate().update("delete from users where id = ?", id);
		
	}

}
